package com.beta.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.beta.dto.product.ProductItemDto;
import com.beta.service.IProductItemsService;
import com.beta.dto.home.HomeRecommendsDto;
import com.beta.entity.HomeRecommends;
import com.beta.entity.Users;
import com.beta.mapper.HomeRecommendsMapper;
import com.beta.service.IDateTimeService;
import com.beta.service.IHomeRecommendsService;
import com.beta.service.ServiceException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * HomeRecommends 表数据服务层接口实现类
 */
@Service
public class HomeRecommendsService extends SuperServiceImpl<HomeRecommendsMapper, HomeRecommends> implements IHomeRecommendsService {

    @Autowired
    private IProductItemsService productItemsService;
    @Autowired
    private IDateTimeService dateTimeService;

    @Override
    public List<HomeRecommends> getHomeRecommends(Users user) {
        if (Objects.isNull(user)) {
            throw new ServiceException("E00113");
        }
        String cityCode = user.getCityCode();
        HomeRecommends selectEntity = new HomeRecommends().setCityCode(cityCode);
        List<HomeRecommends> homeRecommends = selectList(new EntityWrapper<>(selectEntity).orderBy("position"));

        return homeRecommends;
    }

    @Override
    public Page<HomeRecommendsDto> getHomeRecommendsPage(Page<HomeRecommendsDto> page) {
        List<HomeRecommendsDto> homeRecommendsDtos = baseMapper.selectHomeRecommendsPage(page);
        fillProductItemInfo(homeRecommendsDtos);
        page.setRecords(homeRecommendsDtos);
        return page;
    }

    @Override
    public HomeRecommendsDto getHomeRecommendById(Integer id) {
        HomeRecommendsDto homeRecommendsDto = baseMapper.selectHomeRecommendById(id);
        if (Objects.isNull(homeRecommendsDto)) {
            return null;
        }
        fillProductItemInfo(Arrays.asList(homeRecommendsDto));
        return homeRecommendsDto;
    }

    private void fillProductItemInfo(List<HomeRecommendsDto> homeRecommends) {
        if (CollectionUtils.isEmpty(homeRecommends)) {
            return ;
        }

        List<Integer> productItemIds = homeRecommends.stream()
                .filter(homeRecommend -> Objects.nonNull(homeRecommend.getProductItemId()))
                .map(HomeRecommendsDto::getProductItemId)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(productItemIds)) {
            return ;
        }

        List<ProductItemDto> productItems = productItemsService.getProductItemsBatchByIds(productItemIds);
        if (CollectionUtils.isEmpty(productItems)) {
            return ;
        }

        Map<Integer, ProductItemDto> productItemMap = productItems.stream()
                .collect(Collectors.toMap(ProductItemDto::getId, Function.identity()));
        homeRecommends.forEach(homeBanner -> {
            homeBanner.setProductItem(productItemMap.get(homeBanner.getProductItemId()));
        });
    }

    @Override
    public void createHomeRecommend(HomeRecommendsDto homeRecommendsDto) {
        HomeRecommends homeRecommends = parseHomeRecommend(homeRecommendsDto);
        appendProductItemInfo(homeRecommends, homeRecommendsDto.getProductItemId());
        homeRecommends.setCreatedAt(dateTimeService.toDbDate());

        insertSelective(homeRecommends);
    }

    private void appendProductItemInfo(HomeRecommends homeRecommends, Integer productItemId) {
        ProductItemDto productItem = productItemsService.getProductItem(productItemId);

        homeRecommends.setProductId(productItem.getProductId());
        homeRecommends.setProductItemId(productItemId);
        homeRecommends.setProductName(productItem.getName());
        homeRecommends.setCityCode(productItem.getCityCode());
        homeRecommends.setCityName(productItem.getCityName());
        homeRecommends.setPrice(productItem.getPrice());
        homeRecommends.setUnit(productItem.getChargeUnit());
        homeRecommends.setUserId(productItem.getUserId());
        homeRecommends.setUserName(productItem.getUserName());
    }

    @Override
    public void updateHomeRecommendById(Integer id, HomeRecommendsDto homeRecommendsDto) {
        HomeRecommends homeRecommends = parseHomeRecommend(homeRecommendsDto);
        homeRecommends.setId(id);

        updateSelectiveById(homeRecommends);
    }

    private HomeRecommends parseHomeRecommend(HomeRecommendsDto homeRecommendsDto) {
        HomeRecommends homeRecommends = new HomeRecommends();
        homeRecommends.setRecommendType(homeRecommendsDto.getRecommendType());
        homeRecommends.setPosition(homeRecommendsDto.getPosition());
        homeRecommends.setUpdatedAt(dateTimeService.toDbDate());
        homeRecommends.setIsAvailable(homeRecommendsDto.isAvailable() ? 1 : 0);
        return homeRecommends;
    }

}